<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require(ROOT_PATH . 'includes/cls_image.php');
    $allow_file_types = '|DOC|DOCX|';
	$name = $_REQUEST["name"];
	$type = $_REQUEST["type"];
	$error = "";
	$msg = "";
	if(!empty($_FILES["resumefile"]['error']))
	{
		switch($_FILES["resumefile"]['error'])
		{

			case '1':
				$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
				break;
			case '2':
				$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
				break;
			case '3':
				$error = 'The uploaded file was only partially uploaded';
				break;
			case '4':
				$error = '请上传附件.';
				break;

			case '6':
				$error = 'Missing a temporary folder';
				break;
			case '7':
				$error = 'Failed to write file to disk';
				break;
			case '8':
				$error = 'File upload stopped by extension';
				break;
			case '999':
			default:
				$error = 'No error code avaiable';
		}
	}
	elseif(empty($_FILES['resumefile']['tmp_name']) || $_FILES['resumefile']['tmp_name'] == 'none')
	{
		$error = '请上传附件..';
	}
	else 
	{
		// 检查文件格式
        if (!check_file_type($_FILES['resumefile']['tmp_name'], $_FILES['resumefile']['name'], $allow_file_types))
        {
            $error = "上传的文件类型不合法";
        }
        
		$maxsize = maxsize($type);
		
		if(@filesize($essayfile['tmp_name']) > $maxsize)
		{
			$error = "上传文件超过最大限制";
		}
        // 复制文件
        $res = save_upload_file($_FILES['resumefile'], $type);
        if ($res != false)
        {
        	$msg = $_FILES['resumefile']['name']."上传成功";
        }
	}
	echo "{";
	echo				"error: '" . $error . "',\n";
	echo				"msg: '" . $msg . "'\n";
	echo "}";

function maxsize($type)
{
	$sql = "SELECT maxsize FROM " . $GLOBALS["ecs"]->table("service") . " WHERE type = '$type'";
	$maxsize = $GLOBALS["db"]->getOne($sql);
	$maxsize = $maxsize * 1024 * 1024;
	return $maxsize;
}


/* 上传文件 */
function save_upload_file($upload, $type)
{
	$path = DATA_DIR . "/user/".$_SESSION["user_id"]."/";
	
    if (!make_dir($path))
    {
        /* 创建目录失败 */
        return false;
    }

    $filename = @cls_image::random_filename() . substr($upload['name'], strpos($upload['name'], '.'));
    $path .= $filename;
    if (move_upload_file($upload['tmp_name'], $path))
    {
	   	$where = " WHERE type = '{$type}' AND resumefile != '' AND session_id = '".SESS_ID."'";
    	$sql = "SELECT resumefile FROM " . $GLOBALS['ecs']->table("temp_file") . $where;
    	$resumefile = $GLOBALS['db']->getOne($sql);
    	
    	if(is_file($resumefile))
    	{
    		unlink($resumefile);	
    	}
    	
       $GLOBALS['db']->query("DELETE FROM " . $GLOBALS['ecs']->table("temp_file"). $where);
       
       $data = array(
       		'session_id' => SESS_ID,
       		'type'       => $type,
       		'resumefile'  => $path,
       );
       return $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table("temp_file"), $data, "INSERT");
    }
    else
    {
        return false;
    }
}
?>